require "TSLib"
require("config")

textDict = addTSOcrDict("1.txt")
local T5888 = {766,218}
local startPosition = {1110, 179}
local slidePosition = {500, 500}
local addDeleValue = 1000

local options = {
    ["dim"] = 90,
    ["flag"] = false
}
pk = {}


-- 屏幕中心点
local postionLeft = {1113, 653}

-- 开始游戏
local function playGameS()
    tap(postionLeft[1], postionLeft[2])
    mSleep(1000)
    local colorPosi = {
        {  800,  314, 0x58585d},
        {  800,  346, 0x65656c},
        {  803,  398, 0x58585d},
        {  811,  473, 0xdfdfe9},
        {  814,  264, 0xc7c7da},
    }
local x, y = findMultiColorInRegionFuzzyByTable(colorPosi,90,777,500,859,217)
if x ==-1 and y ==-1 then
    pk.seePkI()
else
    tap(postionLeft[1], postionLeft[2])
    mSleep(1000)
    tap(927, 357)
    toast("进入pk")
    pk.Init()
end 
end


-- 查找pk
function pk.seePkI()
    --读取脚本配置文件内容
    textList = readUIConfigFile(config.configPath)
    local count = textList["numberGame"]
    for k, v in pairs(textList) do
        -- dialog(k..":"..v,5)
        if k == "numberGame" then 
            if textList["numberGame"] ~= "0" then
                textList["numberGame"] = count - 1
                utils.setConfig(textList)
                playGameS()
            else 
                -- 退出脚本
                lua_exit()
            end
        end
    end
end

-- 对方是否在游戏中
function otherPartyTheGame()
    local otherColor = {
        {  327,  338, 0xfffcaf},
        {  336,  455, 0xffe263},
        {  455,  172, 0x61497a},
        {  434,  539, 0x594172},
        { 1096,  443, 0x0088e1},
    }
if multiColor(otherColor,options) then
    tap(1104, 365)
    mSleep(1000)
    pk.seePkI()
else
    pk.seePkI()
end  
end
-- 滑动
function gameSlide()
    -- 直接顶到顶部
    -- touchDown(slidePosition[1], slidePosition[2])
    -- mSleep(50)
    -- touchMove(0, slidePosition[1])
    -- mSleep(50)
    -- touchUp(0, slidePosition[1])
    -- mSleep(50)
    
    
    -- 先向左
    touchDown(slidePosition[1], slidePosition[2])
    mSleep(50)
    touchMove(slidePosition[1] - addDeleValue, slidePosition[2])
    mSleep(50)
    touchUp(slidePosition[1] - addDeleValue, slidePosition[2])
    mSleep(50)
    -- 向上
    touchDown(slidePosition[1] -addDeleValue, slidePosition[2])
    mSleep(50)
    touchMove(slidePosition[1] - addDeleValue, slidePosition[2] + 50)
    mSleep(50)
    touchUp(slidePosition[1] - addDeleValue, slidePosition[2] + 50)
    mSleep(50)
    -- 向右
    touchDown(slidePosition[1] -addDeleValue, slidePosition[2] + 50)
    mSleep(50)
    touchMove(slidePosition[1] + addDeleValue, slidePosition[2] + 50)
    mSleep(50)
    touchUp(slidePosition[1] + addDeleValue, slidePosition[2] + 50)
    mSleep(50)
    -- 向下
    touchDown(slidePosition[1], slidePosition[2])
    mSleep(50)
    touchMove(slidePosition[1], slidePosition[2] - 50)
    mSleep(50)
    touchUp(slidePosition[1], slidePosition[2] - 50)
    mSleep(50)
end

-- 是否结束游戏
function isGameOver()
    local color_tableIs = {
        {   23,  165, 0xfcd85d},
        {  185,  133, 0xd0650d},
        {  193,  668, 0xfbd554},
        {   38,  600, 0xfbd34c},
        {  422,  365, 0xfbd863},
    }
if multiColor(color_tableIs,options) then--全部坐标点和颜色一致时返回 true，== true 可省略不写
    tap(1001, 359)
    mSleep(5000)
    tap(1104, 648)
    mSleep(1000)
    pk.seePkI()
else
    playGame()
    mSleep(5000)
    gameSlide()
    mSleep(1000)
    isGameOver()
end
end

function playGame()
    -- 点击技能
    tap(960, 666)
    mSleep(500)
    tap(1113, 666)
    mSleep(500)
    tap(788, 50)
    mSleep(500)
    tap(945, 52)
    mSleep(500)
    tap(1097, 54)
    mSleep(500)
end

-- 是否进入了游戏
function isGoGame()
    -- 先查询是否进入了游戏了
    local color_table = {
        {  132,  672, 0xe95608},
        {  133,  693, 0xfff8e7},
        {  130,  631, 0xea5609},
        {  128,  543, 0xea580a},
    }
if multiColor(color_table,options) then--全部坐标点和颜色一致时返回 true，== true 可省略不写
    toast("游戏已经开始了")
    isGameOver()
else
    toast("游戏未开始呢!")
    mSleep(1000)
    isGoGame()
    tap(startPosition[1], startPosition[2])
end   
end
function pk.Init()
    mSleep(1000)
    tap(T5888[1], T5888[2])
    mSleep(1000)
    tap(startPosition[1], startPosition[2])
    mSleep(1000)
    tap(startPosition[1], startPosition[2])
    isGoGame()
end

return pk